package org.symkom.card;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import org.symkom.expression.Addition;
import org.symkom.expression.Constant;
import org.symkom.expression.Expression;
import org.symkom.expression.Multiplication;
import org.symkom.expression.Operation;
import org.symkom.expression.ParameterT;
import org.symkom.expression.ParameterX;
import org.symkom.expression.SKObject;
import org.symkom.main.MainWindow;
import org.symkom.states.States;

public class FunctionPane extends Card{
	
	String functionStart;
	JLabel functionLabel;
	
	JPanel buttonsPane;
	JButton[] buttons;
	
	private JPanel midPane;
	private JPanel labelPane;
	
	private JPanel titlePane;
	private JPanel middlePane;
	private JPanel navigationPane;
	
	public FunctionPane(MainWindow mainWindow, Card prevCard) {
		super(mainWindow, prevCard);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void init() {
		super.init();
		
		setLayout(new BorderLayout());
		
		titlePane = new JPanel(new FlowLayout());
		middlePane = new JPanel(new FlowLayout());
		navigationPane = new JPanel(new FlowLayout());
		
		titlePane.add(new JLabel("Podaj wzor rownania rozniczkowego: "));
		
		midPane = new JPanel(new BorderLayout());
		//buttons;
		buttonsPane = new JPanel(new FlowLayout());
		buttons = new JButton[6];
		buttons[0] = new JButton(Addition.getFormula());
		buttons[1] = new JButton(Multiplication.getFormula());
		buttons[2] = new JButton(Constant.getFormula());
		buttons[3] = new JButton(ParameterX.getFormula());
		buttons[4] = new JButton(ParameterT.getFormula());
		buttons[5] = new JButton("delete");
		
		for(JButton b : buttons) {
			b.addActionListener(new ExpressionListener());
			buttonsPane.add(b);
		}
		
		//label
		labelPane = new JPanel(new FlowLayout());
		functionLabel = new JLabel();
		functionStart = "x'(t)= ";
		functionLabel.setText(buildAllLabel());
		labelPane.add(functionLabel);
		
		midPane.add(buttonsPane, BorderLayout.NORTH);
		midPane.add(labelPane, BorderLayout.CENTER);
		middlePane.add(midPane);
		
		navigationPane.add(nextButton);
		
		add(titlePane, BorderLayout.NORTH);
		add(middlePane, BorderLayout.CENTER);
		add(navigationPane, BorderLayout.SOUTH);
	}
	private class ExpressionListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			String text = functionLabel.getText();
			
			JButton but = (JButton)e.getSource();
			for(int i = 0 ; i < buttons.length ; i++)
				if(but.equals(buttons[i])) {
									
					SKObject sko = null;
					
					switch(i) {
					case 0:
						sko = new Addition();
						break;
					case 1:
						sko = new Multiplication();
						break;
					case 2:
						sko = new Constant();
						break;
					case 3:
						sko = new ParameterX();
						break;
					case 4:
						sko = new ParameterT();
						break;
					//delete
					case 5:	
						if(!function.isEmpty())
							text = delete();
						break;
					}
					if(sko != null) {
						text += " " + sko.getOwnFormula();
						function.add(sko);
					}
					
				}
			functionLabel.setText(text);
		}
		
	}
	private String delete() {
		SKObject sk = function.get(function.size()-1);
		sk.delete();
		function.remove(sk);
					
		return buildAllLabel();
	}
	private String buildAllLabel() {
		String text = functionStart;
		for(SKObject s : function)
			text += " " + s.getOwnFormula();
		return text;
	}
	@Override
	protected void nextStage() {
		if(validation()) {
			mainWindow.setState(States.PARAMETER, this);
		}
		else 
			JOptionPane.showMessageDialog(this, "Invalid expression");
		
	}
	private boolean validation() {
		if(function.isEmpty()) return false;
		if(function.size() % 2 == 0) return false;
		
		for(int i = 0 ; i < function.size() ; i++) {
			if(i%2 == 0) {
				if(function.get(i) instanceof Operation)
					return false;
			}
			else
				if(function.get(i) instanceof Expression)
					return false;
		}
		
		return true;
		
	}
}
